{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "05b88e19",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 音频和视频互转"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "285a597d",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "<font style=\"color:red;font-size:20px\">1. 视频 => 音频 ：提取视频中的声音</font>\n",
    "\n",
    "<font style=\"color:red;font-size:20px\"> 2. 音频 => 视频 ：一张图片 + 音频 => 视频</font>\n",
    "\n",
    "<font style=\"color:red;font-size:20px\">3. 音频 => 视频 ：多张图片 + 音频 => 视频</font>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33302709",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 导入包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd955f14",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from IPython.display import Video, Audio, Image\n",
    "import os\n",
    "from av_utils import av_trim, show_imgs, v_to_mp3, ia_to_mp4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f380b3e",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 1.  视频 => 音频 ：提取视频中的声音\n",
    "## 1.1 原始视频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b47d5262",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "input_mp4_file = 'video/少年_片段.mp4'  # 原始视频文件\n",
    "\n",
    "print(\"原始视频路径：\", os.path.abspath(input_mp4_file))\n",
    "\n",
    "# 看看原视频\n",
    "Video(input_mp4_file, width=320, height=180, embed=True)   "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cba4d8e9",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 1.2 提取视频的声音"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9bf57ec5",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 提取后的音频路径\n",
    "mp3_file = \"output/少年_片段.mp3\"\n",
    "\n",
    "print(\"输入视频路径：\", os.path.abspath(input_mp4_file))\n",
    "print(\"     ↓↓↓\")\n",
    "print(\"输出音频路径：\", os.path.abspath(mp3_file))\n",
    "\n",
    "# v to mp3（视频 video 转 mp3）\n",
    "v_to_mp3(input_mp4_file , mp3_file)\n",
    "\n",
    "# 听听铃声效果\n",
    "Audio(mp3_file, autoplay=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bce972a7",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 2. 音频 => 视频，一张图片和音频合成视频\n",
    "## 2.1 查看图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07ec6e99",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 图片路径\n",
    "input_image_file = \"images/cat.jpg\"\n",
    "\n",
    "print(\"图片路径：\", os.path.abspath(input_image_file))\n",
    "\n",
    "# 查看图片\n",
    "Image(filename=input_image_file,width=100,height=200)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b4e9473",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 2.2 合成视频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e2f06f0",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 输出的 mp4 视频文件路径\n",
    "output_mp4_file =  'output/magazine.mp4'\n",
    "\n",
    "print(\"输入图片路径：\", os.path.abspath(input_image_file))\n",
    "print(\"输入音频路径：\", os.path.abspath(mp3_file))\n",
    "print(\"     ↓↓↓\")\n",
    "print(\"输出视频路径：\", os.path.abspath(output_mp4_file))\n",
    "\n",
    "# i a to mp4（图片 image 音频 audio 转 视频 mp4）\n",
    "ia_to_mp4(input_image_file, mp3_file, output_mp4_file)\n",
    "\n",
    "# 查看合成的视频效果\n",
    "Video(output_mp4_file, width=320, height=184, embed=True)   "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83bc429a",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 3. 音频 => 视频，多张图片和音频合成视频\n",
    "## 3.1 查看图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48374246",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 多张输入图片\n",
    "input_image_files = [\"images/cat.jpg\", \"images/duck.jpg\", \"images/bear.jpg\"]\n",
    "\n",
    "# 查看图片\n",
    "show_imgs(input_image_files, cols=3, rows=1, figsize=(4, 3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62948aaa",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 3.2 合成视频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a17bb24b",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 输出的 mp4 视频文件路径\n",
    "output_mp4_file =  'output/magazine_all.mp4'\n",
    "\n",
    "for img_file in input_image_files:\n",
    "    print(\"输入图片路径：\", os.path.abspath(img_file))\n",
    "print(\"输入音频路径：\", os.path.abspath(mp3_file))\n",
    "print(\"     ↓↓↓\")\n",
    "print(\"输出视频路径：\", os.path.abspath(output_mp4_file))\n",
    "\n",
    "# i a to mp4（图片 image 音频 audio 转 视频 mp4）\n",
    "ia_to_mp4(input_image_files, mp3_file, output_mp4_file)\n",
    "\n",
    "# 查看合成的视频效果\n",
    "Video(output_mp4_file, width=320, height=184, embed=True)   "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
